﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameUIController : MonoBehaviour {
	// 单例模式
	private static GameUIController m_Instance = null;
	public static GameUIController Instance {
		get {
			if(m_Instance == null) {
				m_Instance = GameObject.FindObjectOfType(typeof(GameUIController)) as GameUIController;
			}

			if (!m_Instance) {
				GameObject obj = new GameObject("GameUIController");
				m_Instance = obj.AddComponent<GameUIController>();
			}
			return m_Instance;
		}
	}
	

	[Tooltip("外框")]
	public GameObject boader;

	[Tooltip("滚动时间间隔")]
	public float playInterval = 0.3f;

	public Text IntervalText;
	public Text EnergyText;
	public Text VirusNumText;
	public Text TissueCellNumText;
    public GameObject overText;
    public GameObject overButton;
    private bool scrollActive = true;

	private float[] posArray = { -148, 0, 148 };
	public int CurrentCellIndex{
		get; private set;
	}

	private void Awake() {
		CurrentCellIndex = 1;
	}

	private void Update () {
		float axis;
		if ((axis = Input.GetAxis("Mouse ScrollWheel")) != 0 && scrollActive) {
			scrollActive = false;
			Invoke ("activeScroll", playInterval);
			// 加上3为了放置-1后小于0导致取模错误
			if (axis > 0) {
				CurrentCellIndex = (CurrentCellIndex - 1 + 3) % 3;
			} else if (axis < 0) {
				CurrentCellIndex = (CurrentCellIndex + 1 + 3) % 3;
			}

			Vector2 targetPos = new Vector2 (posArray [CurrentCellIndex], boader.transform.localPosition.y);
			boader.transform.localPosition = Vector2.MoveTowards(boader.transform.localPosition, targetPos, 1000f);
		}

		IntervalText.text = "" + GameController.Instance.CurrentInterval;
		EnergyText.text = "" + GameController.Instance.CurrentEnergy;
		// VirusNumText.text = GameController.Instance.CurrentNumberOfPathogens + "/" + GameController.Instance.MaxNumberOfPathogens;
		VirusNumText.text = "" + GameController.Instance.CurrentNumberOfPathogens;
		TissueCellNumText.text = "" + GameObject.FindGameObjectsWithTag("TissueCell").Length;
	}

	private void activeScroll() {
		scrollActive = true;
	}
    public void activeOverButton()
    {
        overButton.SetActive(true);
    }
    public void winText()
    {
        overText.GetComponent<Text>().text = "Heal";
        overText.SetActive(true);
    }
    public void lostText()
    {
        overText.GetComponent<Text>().text = "Treatment failure";
        overText.SetActive(true);
    }
}
